
package samples;
// {{{ imports
import jp.ize.ux.UXRoot;
import jp.ize.ux.UXFocusManager;
import jp.ize.ux.components.UXBasicComponent;
import jp.ize.ux.components.UXCell;
import jp.ize.ux.components.UXTable;
import jp.ize.ux.components.UXLabel;
import jp.ize.ux.components.UXTextField;
import jp.ize.ux.components.UXSelector;
import jp.ize.ux.components.UXItem;
import jp.ize.ux.components.UXComponentResourceFactory;
//import jp.ize.ux.states.UXTableState;
import jp.ize.ux.acts.MoveAct;
import jp.ize.ux.acts.TableLayoutAct;
import jp.ize.ux.acts.FocusAct;
import jp.ize.ux.interactions.UXKeyInteraction;
import jp.ize.ux.events.UXMouseEvent;
import jp.ize.ux.events.UXKeyboardEvent;
import jp.ize.ux.tween.easings.Linear;
import jp.ize.ux.tween.easings.Expo;
import jp.ize.ux.geom.UXPoint;
import jp.ize.ux.geom.UXSize;
import jp.ize.iwp.RuntimeEngine;
import jp.ize.iwp.utils.DebugUtil;
import flash.display.Loader;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.SecurityErrorEvent;
import flash.net.URLRequest;
import flash.system.ApplicationDomain;
import flash.system.LoaderContext;
import flash.ui.Keyboard;
// }}}
// {{{ class Table
class Table
{
	// {{{ public static function main() : Void
	/**
	*
	*/
	public static function main() : Void
	{
		RuntimeEngine.init();
		DebugUtil.init();
		DebugUtil.setLocation( 80, 350 );
		var t = new Table();
	}

	// }}}
	// {{{ public function new() : Void

	public function new() : Void
	{
		load();
	}

	// }}}
	// {{{ public function load() : Void

	public function load() : Void
	{
		var loader = new Loader();
		var me = this;
		loader.contentLoaderInfo.addEventListener( SecurityErrorEvent.SECURITY_ERROR, function(e) {
			trace("error: " + e);
		});
		loader.contentLoaderInfo.addEventListener( IOErrorEvent.IO_ERROR, function(e) {
			trace("error: " + e);
		});
		loader.contentLoaderInfo.addEventListener( Event.COMPLETE, function(e){
			me.onComplete();
		});
		loader.contentLoaderInfo.addEventListener( Event.INIT, function(e){
		});
		RuntimeEngine.getStage().addChild(loader);

		loader.load(new URLRequest("res.swf"),new LoaderContext(false,ApplicationDomain.currentDomain));
		//var res = haxe.Resource.getBytes("btn");
		//loader.loadBytes(res.getData(), new LoaderContext(false,ApplicationDomain.currentDomain));
	}

	// }}}
	// {{{ public function onComplete() : Void

	public function onComplete() : Void
	{
		// {{{ index
		var comp001 = new UXLabel("== 1 ==",24,0xFFFFFF);
		var comp002 = new UXLabel("== 2 ==",24,0xFFFFFF);
		var comp003 = new UXLabel("== 3 ==",24,0xFFFFFF);
		var comp004 = new UXLabel("== 4 ==",24,0xFFFFFF);
		var comp005 = new UXLabel("== 5 ==",24,0xFFFFFF);
		// }}}
		// {{{ field 1
		var comp1 = new UXLabel("名前",16,0xFFFFFF);
		var comp2 = new UXTextField("",16,0xFFFFFF,new UXSize(100,20));
		// }}}
		// {{{ field 2
		var comp3 = new UXLabel("年齢",16,0xFFFFFF);
		var comp4 = new UXTextField("",16,0xFFFFFF,new UXSize(100,20));
		// }}}
		// {{{ field 3
		//var comp5 = new UXLabel("職業",16,0xFFFFFF);
		var comp5 = new UXLabel("性別",16,0xFFFFFF);
		//var comp6 = new UXTextField("",16,0xFFFFFF,new UXSize(100,20));
		// }}}
		// {{{ field 4
		var comp7 = new UXLabel("電話番号",16,0xFFFFFF);
		var comp8 = new UXTextField("",16,0xFFFFFF,new UXSize(100,20));
		// }}}
		// {{{ field 5
		var comp9 = new UXLabel("備考",16,0xFFFFFF);
		var comp10 = new UXTextField("",16,0xFFFFFF,new UXSize(100,20));
		// }}}

		// {{{ selector
		var res1 = UXComponentResourceFactory.create("Male");
		var res2 = UXComponentResourceFactory.create("Female");
		var selector = new UXSelector(EXCLUSIVE);
		var item1 = new UXItem(res1,null,"男");
		var item2 = new UXItem(res2,null,"女");
		item1.x = 10;
		item1.y = -20;
		item2.x = 60;
		item2.y = -20;
		selector.addUXChild(item1);
		selector.addUXChild(item2);
		var cur:Int = 0;
		var inter1 = new UXKeyInteraction(Keyboard.LEFT);
		inter1.addAct( function(s:Dynamic){
			trace("left");
			cur--;
			if ( cur<0 ) cur = Std.int(s.items.length-1);
			s.eventTarget = s.items[cur];
		});
		inter1.bind("Select");
		var inter2 = new UXKeyInteraction(Keyboard.RIGHT);
		inter2.addAct( function(s:Dynamic){
			trace("right");
			cur++;
			if ( cur>s.items.length-1 ) cur = 0;
			s.eventTarget = s.items[cur];
		});
		inter2.bind("Select");
		selector.ctrl.addInteraction(inter1);
		selector.ctrl.addInteraction(inter2);
		// }}}
		var comp6 = selector;

		// {{{ table
		var table = new UXTable( new UXSize(300,300), 5, 3 );
		table.x = 450;
		table.y = 200;
		// }}}
		// {{{ focus manager
		var fm = new UXFocusManager();
		fm.addFocusee(comp2,0);
		fm.addFocusee(comp4,1);
		fm.addFocusee(comp6,2);
		fm.addFocusee(comp8,3);
		fm.addFocusee(comp10,4);
		// }}}

		// {{{ acts and parameters
		var act  = new MoveAct();
		var act2 = new TableLayoutAct();
		var param = {
			time: 1.0,
			transition: Linear.easeOut,
			p0: new UXPoint(450,200),
			p1: new UXPoint(400,0),
			p2: new UXPoint(100,100)
		};
		var param2 = {
			time: 1.5,
			transition: Expo.easeIn
		};
		var param3 = {
			time: 0.3,
			transition: Expo.easeOut,
			dir: ROW,
			delta: -1
		};
		// }}}
		// {{{ interaction
		table.ctrl.getInteraction("Layout").addReactEvent( UXMouseEvent.DOWN );
		table.ctrl.getInteraction("Layout").addAct( act.curvingMove(param) );
		table.ctrl.getInteraction("Layout").addAct( act2.layout(param2) );
		table.ctrl.getInteraction("Layout").addFinishHandler(
			function(e){
				fm.setFocusIndex(0);
				table.ctrl.getInteraction("Layout").removeReactEvent( UXMouseEvent.DOWN );
				table.ctrl.getInteraction("Layout").disable();
			}
		);

		table.ctrl.getInteraction("Action").judge = function(s):Bool {
			if ( s.keyboard.keyCode==Keyboard.ENTER ) {
				var t:Dynamic = null;
				t = s.eventTarget;
				if ( t.parent.y==120 ) {
					fm.next();
				}
				return true;
			} else {
				return false;
			}
		};
		table.ctrl.getInteraction("Action").addAct( act2.move(param3) );
		// }}}
	
		// {{{ add item at table
		table.addItem( new UXCell(comp004) ); // [nearly equal] table.setItem(0,0,new UXCell(...))
		table.addItem( new UXCell(comp7) );   // [nearly equal] table.setItem(0,1,new UXCell(...))
		table.addItem( new UXCell(comp8) );   // [nearly equal] table.setItem(0,2,new UXCell(...))
		table.addItem( new UXCell(comp005) ); // [nearly equal] table.setItem(1,0,new UXCell(...))
		table.addItem( new UXCell(comp9) );   // [nearly equal] table.setItem(1,1,new UXCell(...))
		table.addItem( new UXCell(comp10) );  // [nearly equal] table.setItem(1,2,new UXCell(...))
		table.addItem( new UXCell(comp001) ); // [nearly equal] table.setItem(2,0,new UXCell(...))
		table.addItem( new UXCell(comp1) );   // [nearly equal] table.setItem(2,1,new UXCell(...)) 
		table.addItem( new UXCell(comp2) );   // [nearly equal] table.setItem(2,2,new UXCell(...))
		table.addItem( new UXCell(comp002) ); // [nearly equal] table.setItem(3,0,new UXCell(...))
		table.addItem( new UXCell(comp3) );   // [nearly equal] table.setItem(3,1,new UXCell(...))
		table.addItem( new UXCell(comp4) );   // [nearly equal] table.setItem(3,2,new UXCell(...))
		table.addItem( new UXCell(comp003) ); // [nearly equal] table.setItem(4,0,new UXCell(...))
		table.addItem( new UXCell(comp5) );   // [nearly equal] table.setItem(4,1,new UXCell(...))
		table.addItem( new UXCell(comp6) );   // [nearly equal] table.setItem(4,2,new UXCell(...))
		// }}}

		// {{{ focus window object
		var obj = new UXBasicComponent();
		obj.graphics.lineStyle( 2.0, 0xFFFF00 );
		obj.graphics.drawRoundRect( 85, 200, 330, 60, 5 );
		// }}}
		
		table.ctrl.getInteraction("Action").addReactEvent( UXKeyboardEvent.KEY_DOWN );

		UXRoot.addUXChild( table );
		UXRoot.addUXChild( obj );
	}

	// }}}
}
// }}}
